package de.bahn.dbs.diva;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.security.PrivateKey;
import java.security.Signature;
import java.util.Calendar;

public class SignatureTest {

	public static void signFile(String fileKey, String fileSign) {

		PrivateKey privateKey = null;
		int i = 1;

		// 2. Private Key lesen
		try {
			FileInputStream file = new FileInputStream(fileKey);

			ObjectInputStream o = new ObjectInputStream(file);

			privateKey = (PrivateKey) o.readObject();
			o.close();

		} catch (IOException e) {
			System.err.println(e);
		} catch (ClassNotFoundException e) {
			System.err.println(e);
		}

		// 3. Signatur erstellen
		try {
			long start = Calendar.getInstance().getTimeInMillis();

			// Signatur-Objekt erstellen
			Signature signature;
			signature = Signature.getInstance("SHA1with"
					+ privateKey.getAlgorithm());
			signature.initSign(privateKey);
			// Eingabedatei einlesen
			FileInputStream in = new FileInputStream(fileSign);
			int len;
			byte[] data = new byte[1024];
			while ((len = in.read(data)) > 0) {
				// Signatur updaten
				signature.update(data, 0, len);
			}
			in.close();
			// Signatur berechnen
			byte[] result = signature.sign();

			// Signatur ausgeben
			FileOutputStream out = new FileOutputStream(fileSign + "_"
					+ privateKey.getAlgorithm() + "_" + i++ + ".sig");
			out.write(result, 0, result.length);
			out.close();

			long end = Calendar.getInstance().getTimeInMillis();

			// System.out.println("Signatur: " + signature.);
			// System.out.println("Key2: " + privateKey.getAlgorithm()
			// + "\nZeit: " + (end - start));

			// Log.i("Signature", signature.getProvider().toString());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 4. Signatur speichern
		// Toast.makeText(this, "Signatur fertig", Toast.LENGTH_LONG).show();
	}

	public static String test(String size) {

		long start, end;
		String result = "";

		result += "Test 1: RSA 1024, " + size + " File \n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_RSA_1024", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#1: " + (end - start) + "ms\n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_RSA_1024", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#2: " + (end - start) + "ms\n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_RSA_1024", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#3: " + (end - start) + "ms\n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_RSA_1024", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#4: " + (end - start) + "ms\n";

		// Test 2
		result += "Test 2: RSA 1728, " + size + " File \n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_RSA_1728", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#1: " + (end - start) + "ms\n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_RSA_1728", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#2: " + (end - start) + "ms\n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_RSA_1728", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#3: " + (end - start) + "ms\n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_RSA_1728", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#4: " + (end - start) + "ms\n";

		// Test 3:
		result += "Test 3: RSA 2048, " + size + " File \n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_RSA_2048", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#1: " + (end - start) + "ms\n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_RSA_2048", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#2: " + (end - start) + "ms\n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_RSA_2048", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#3: " + (end - start) + "ms\n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_RSA_2048", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#4: " + (end - start) + "ms\n";

		// Test 4:
		result += "Test 4: DSA 1024, " + size + " File \n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_DSA_1024", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#1: " + (end - start) + "ms\n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_DSA_1024", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#2: " + (end - start) + "ms\n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_DSA_1024", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#3: " + (end - start) + "ms\n";

		start = Calendar.getInstance().getTimeInMillis();
		SignatureTest.signFile("/sdcard/keys/private_DSA_1024", "/sdcard/keys/"
				+ size + ".txt");
		end = Calendar.getInstance().getTimeInMillis();
		result += "#4: " + (end - start) + "ms\n";

		return result;
	}
}
